package com.example.service;

import com.example.dao.BackendApiDao;
import com.example.entity.BackendApi;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BackendApiService {

    @Autowired
    BackendApiDao backendApiDao;

    public List<BackendApi> getApis() {
        return backendApiDao.getAllApis();
    }
    
    public PageInfo<BackendApi> getApisPage(int pageNum, int pageSize, String orderBy) {
        PageHelper.startPage(pageNum, pageSize);
        if (orderBy != null && !orderBy.isEmpty()) {
            PageHelper.orderBy(orderBy);
        }
        List<BackendApi> apis = backendApiDao.getAllApis();
        return new PageInfo<>(apis);
    }
    
    public PageInfo<BackendApi> searchApisByName(String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BackendApi> apis = backendApiDao.searchApisByName(name);
        return new PageInfo<>(apis);
    }
    
    public BackendApi getApiById(int id) {
        return backendApiDao.getApiById(id);
    }
    
    public boolean saveApi(BackendApi backendApi) {
        if (backendApi.getId() > 0) {
            return backendApiDao.updateApi(backendApi) > 0;
        } else {
            return backendApiDao.insertApi(backendApi) > 0;
        }
    }
    
    public boolean deleteApi(int id) {
        return backendApiDao.deleteApi(id) > 0;
    }
}
